﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("inpout32.dll", EntryPoint = "Out32")]
        public static extern void Output(int adress, int value);

        [DllImport("inpout32.dll", EntryPoint = "Out32")]
        public static extern int Input(int adress);

        
        public bool[] tabValue = new bool[8]{false,false,false,false,false,false,false,false};

        public Form1()
        {
            InitializeComponent();
            init();
        }

        private void init()
        {
            listaPortow.Items.Add("LPT1");
            listaPortow.Items.Add("LPT2");
            Output(888, this.convertTab(tabValue));
        }

        private int convertTab(bool[] tabValue)
        {
            int value = 0;
            if (tabValue[0]) value += 1;
            if (tabValue[1]) value += 2;
            if (tabValue[2]) value += 4;
            if (tabValue[3]) value += 8;
            if (tabValue[4]) value += 16;
            if (tabValue[5]) value += 32;
            if (tabValue[6]) value += 64;
            if (tabValue[7]) value += 128;
            return value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (!File.Exists("inpout32.dll"))
            {
                MessageBox.Show("brak pliku inpout32.dll !!!","Błąd krytyczny",MessageBoxButtons.OK,MessageBoxIcon.Error);
                //button1.Enabled = false;
                listaPortow.Enabled = false;
            }
            //int adres = bin2dec(c1.Checked,c2.Checked,c3.Checked,c4.Checked,c5.Checked,c6.Checked,c7.Checked,c8.Checked);
            int lpt1 = 888;
            int lpt2 = 632;
            int lpt = lpt1;
            //MessageBox.Show(Convert.ToString(adres));
            if (listaPortow.Text == "LPT1")
                lpt = lpt1;
            if (listaPortow.Text == "LPT2")
                lpt = lpt2;
            tabValue = new bool[8] { false, false, false, false, false, false, false, false };
            Output(lpt,this.convertTab(tabValue));
            
        }
        
         private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(Input(888).ToString());
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Start();
            Thread.Sleep(1500);
            timer.Stop();
            
        }

        private void Blinker()
        {
            Thread watek = new Thread(new ThreadStart(blink));
            watek.Start();

        }
        private void blink()
        {
            while (true)
            {
                Output(888, 64);
                Thread.Sleep(1000);
                Output(888, 0);
                Thread.Sleep(1000);
                Output(888, 16);
                Thread.Sleep(1000);
                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //this.Blinker();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (tabValue[0] == true)
            {
                tabValue[0] = false;
                btn1.BackColor = Color.Gray;
            }
            else
            {
                tabValue[0] = true;
                btn1.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text=this.convertTab(tabValue).ToString();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (tabValue[1] == true)
            {
                tabValue[1] = false;
                btn2.BackColor = Color.Gray;
            }
            else
            {
                tabValue[1] = true;
                btn2.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (tabValue[2] == true)
            {
                tabValue[2] = false;
                btn3.BackColor = Color.Gray;
            }
            else
            {
                tabValue[2] = true;
                btn3.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (tabValue[3] == true)
            {
                tabValue[3] = false;
                btn4.BackColor = Color.Gray;
            }
            else
            {
                tabValue[3] = true;
                btn4.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (tabValue[4] == true)
            {
                tabValue[4] = false;
                btn5.BackColor = Color.Gray;
            }
            else
            {
                tabValue[4] = true;
                btn5.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (tabValue[5] == true)
            {
                tabValue[5] = false;
                btn6.BackColor = Color.Gray;
            }
            else
            {
                tabValue[5] = true;
                btn6.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (tabValue[6] == true)
            {
                tabValue[6] = false;
                btn7.BackColor = Color.Gray;
            }
            else
            {
                tabValue[6] = true;
                btn7.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (tabValue[7] == true)
            {
                tabValue[7] = false;
                btn8.BackColor = Color.Gray;
            }
            else
            {
                tabValue[7] = true;
                btn8.BackColor = Color.LimeGreen;
            }
            Output(888, this.convertTab(tabValue));
            this.toolStripStatusLabel1.Text = this.convertTab(tabValue).ToString();
        }
        
    }
}
